<?php
print_r($_REQUEST);
	require_once "includes/pagination.php";
	$itemPerPage=10;
?>
<style type="text/css">
	div#list-sort { padding:10px; text-align:right; }
</style>
<script type="text/javascript">
function changefac(val)
{
	var frm = document.frmListPro;
	frm.keyword.value = "sortby";
	//frm.action = "viewusers.php";
	frm.submit();
}
</script>
<div id='panel'>
<form name="frmListPro" action="" method="post">
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Post</h2></td>
  </tr>
  <tr>
    <td>
		
			<input type="hidden" name="keyword">

		 <?php 		   
				   if(isset($_GET['postcat'])) {
				   		$sqlSelPro="SELECT * FROM   tbl_post WHERE  subcategory_id='$_POST[postcat]'";
						$pagelink="products";
				   }
				   else if(isset($_GET['cat'])) {
					   if(!empty($_GET['cat'])) {
						$pagelink="cat=".$_GET['cat'];
						$sqlSelPro="SELECT *  
FROM   tbl_product, tbl_items 
WHERE  tbl_product.product_id = tbl_items.product_id and (tbl_items.category_id=".$_GET['cat'].")";						
						$subCat=getAllSubcategory($_GET['cat']);
							if(count($subCat) > 0 ) { 
							  	$count=strlen($sqlSelPro);
  							 	$sqlSelPro=substr($sqlSelPro,0,($count-1));				   	
								foreach($subCat as $pcatId) {
									$sqlSelPro .=" || tbl_items.category_id=".$pcatId;
								}
								$sqlSelPro.=")";	
								
							}					 
						}
					}
				/*    
				   if($_POST['keyword'] == "sortby")
					{
						if($_POST['fa'] == "name")
						$sqlSelPro .= " order by product_name desc";
						else if($_POST['fa'] == "price")
						$sqlSelPro .= " order by product_price desc";
					} 
					else { $sqlSelPro .= " order by product_id desc"; }
					*/
					
				    if(!empty($_GET['sortby'])) {
				   		if($_GET['sortby']=='lps') {
							$sqlSelPro .= " order by tbl_items.price ASC, tbl_product.shipping ASC";		
						}
						else if($_GET['sortby']=='hps') {
							$sqlSelPro .= " order by tbl_items.price DESC, tbl_product.shipping DESC";		
						}
						else if($_GET['sortby']=='bm') {
							$sqlSelPro .= " order by field(tbl_product.product_name, '$key') DESC";		
						}
						else if($_GET['sortby']=='online') {
							$sqlSelPro .= " order by field(tbl_product.selling_type, 'online') desc";		
						}
						else if($_GET['sortby']=='local') {
							$sqlSelPro .= " order by field(tbl_product.selling_type, 'local') desc";		
						} 
						 $sqlSelPro .=", tbl_items.bargain_duration DESC";	
				   } else { 
				   		$sqlSelPro .=" ORDER BY tbl_items.bargain_duration DESC";	
				   }
				   //echo $sqlSelPro;
				   $rstSelPro=mysql_query(getPagingQuery($sqlSelPro,$itemPerPage));			  
				   $num=mysql_num_rows($rstSelPro);				   
				   if($num==0)
					{
					  echo "No Products Found...";
					}
				   else {
				   ?>
                   <table align="center" width="100%" border="0">
										<tr>
											<td width="9%">Sort by:</td>
											<td width="12%"><a href="index.php?<?php echo $pagelink; ?>&sortby=bm">Best Match</a></td>
											<td width="27%"><a href="index.php?<?php echo $pagelink; ?>&sortby=lps">Lowest Price and Shipping</a></td>
											<td width="29%"><a href="index.php?<?php echo $pagelink; ?>&sortby=hps">Highest Price and Shipping</a></td>
											<td width="11%"><a href="index.php?<?php echo $pagelink; ?>&sortby=online">Online only</a></td>
											<td width="12%"><a href="index.php?<?php echo $pagelink; ?>&sortby=local">Local Only</a></td>
										</tr>
									</table>
                   
                   
                   
                   <?php
				   		echo "";
					    echo "<br><table width='675' border='0' align='center' cellspacing='0'>";
						echo "<tr class='head-tr'>
								  <td height='25' width='120'>&nbsp; </td>
								  <td width='200'> <strong>Product name </strong></td>
								  <td width='100'> <strong>-- </strong></td>
								  <td width='80'> <strong>Price</strong> </td>
							      <td width='165'><strong>Time Left</strong> </td>
								</tr>";
						 while($rowSelPro=mysql_fetch_array($rstSelPro))
					     {
						   $productId=$rowSelPro['product_id'];
						   $itemId=$rowSelPro['item_id'];
						   $productName=wordFormater($rowSelPro['product_name'],20);
						   $productTitle = $rowSelPro['product_name'];					   
						   $productPrice=$rowSelPro['price'];
						   $postedDate=$rowSelPro['posted_date'];
						   $bargainDate = $rowSelPro['bargain_duration'];
						   $timeLeft = getRemainingTime($bargainDate);
						   $pimage = getProductImage($itemId,'i');
						   if($pimage['thumb']!="") {
						   		$productThumb = $pimage['thumb'];
						   } else {
						   		$productThumb="images/no_image.gif";	
						   }
						   
						   if($timeLeft < 0) {
						   		$timeLeft = "<font color='red'>Listing Expired</font>";
						   }
						  
						   					   
						   echo "<tr>
						        <td height='106'>
						   			<div align='center'><img src='$productThumb'></div>
								 </td>								 
								 <td style='vertical-align:top; padding:10px;'> <a href='index.php?products=$itemId' title='$productTitle'> $productName </a> </td>
								 <td style='vertical-align:top; padding:10px;'> <input type='image' src='images/buy.gif' style='cursor:pointer;' onClick=''> </td>
								 <td style='vertical-align:top; padding:10px;'> $$productPrice </td>
								 <td style='vertical-align:top; padding:10px;'> $timeLeft </td>
						       </tr>";	
							echo "<tr>
									 <td colspan='5'> <div style='border-top:1px solid #CCCCCC;'> </div> </td>
								  </tr>";				 	
						 }					 
						 echo " </table>";
				    } 				
	?>
	
  		<tr>
    		<td><div align="center"><?php
				   if(!empty($_GET['sortby']))
				     $link=$pagelink."&sortby=".$_GET['sortby'];
					else
				     $link=$pagelink;
				   echo $disp=getPagingLink($sqlSelPro,$itemPerPage,$link);							   
			 ?></div>
			</td>
  		</tr>
 	</table>
	</form>
	</td>
  </tr>
</table>
</div>	